<template>
  <el-form
    :model="formData"
    :rules="rules"
    label-position="left"
    size="mini"
    ref="form"
    label-width="0"
  >
    <el-form-item prop="oldPassword" label-width="100px">
      <template slot="label">
        <span style="line-height: 40px">旧密码</span>
      </template>
      <el-input size="small" v-model="formData.oldPassword" show-password>
      </el-input>
    </el-form-item>
    <el-form-item prop="password" label-width="100px">
      <template slot="label">
        <span style="line-height: 40px">新密码</span>
      </template>
      <el-input size="small" v-model="formData.password" show-password>
      </el-input>
    </el-form-item>
    <el-form-item prop="password2" label-width="100px">
      <template slot="label">
        <span style="line-height: 40px">重复新密码</span>
      </template>
      <el-input size="small" v-model="formData.password2" show-password>
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        @click="onSubmit"
        :loading="loading"
        class="login-submit"
        style="height: 38px"
        >提交</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
const api = window.$api.organization.user;
export default {
  name: "resetpassword",
  data() {
    return {
      loading: false,
      formData: {
        oldPassword: "",
        password: "",
        password2: "",
      },
      rules: {
        oldPassword: [
          { required: true, message: "请输入旧密码" },
          { validator: this.validatorPassword, trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入新密码" },
          { validator: this.getRule, trigger: "blur" },
        ],
        password2: [
          { required: true, message: "请再输入新密码" },
          {
            validator: (rule, value, callback) => {
              if (this.formData.password2 != this.formData.password) {
                callback(new Error("两次密码输入不一致"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
      passwordType: "password",
    };
  },
  computed: {},
  created() {},
  methods: {
    async getRule(rule, value, callback) {
      // 密级密码验证
      // const secretData = await this.$awaitWraper(api.GetRule());
      //  let passComplexity = secretData.f_Complexity;
      // let passLength = secretData.f_Length;
      // if (passLength > value.length) {
      //   callback(new Error("密码不符合规范"));
      // } else {
      //   // 判断对应密级密码规范
      //   let check;
      //   if (passComplexity.indexOf(1) !== -1) {
      //     // 大写
      //     check = /^(?=.*[A-Z]).*$/;
      //     if (!check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   } else {
      //     check = /^(?=.*[A-Z]).*$/;
      //     if (check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   }
      //   if (passComplexity.indexOf(2) !== -1) {
      //     // 小写
      //     check = /^(?=.*[a-z]).*$/;
      //     if (!check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   } else {
      //     check = /^(?=.*[a-z]).*$/;
      //     if (check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   }
      //   if (passComplexity.indexOf(3) !== -1) {
      //     // 数字
      //     check = /^(?=.*\d).*$/;
      //     if (!check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   } else {
      //     // 没有规范数字，判断是否有数字
      //     check = /^(?=.*\d).*$/;
      //     if (check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   }
      //   if (passComplexity.indexOf(4) !== -1) {
      //     // 特殊字符
      //     check =
      //       /^(?=.*[*?!&￥$%^#,./@";:><\\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
      //     if (!check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   } else {
      //     check =
      //       /^(?=.*[*?!&￥$%^#,./@";:><\\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
      //     if (check.test(value)) {
      //       callback(new Error("密码不符合规范"));
      //     }
      //   }
      //   callback()
      // }

      // 角色密码验证
      const roleData = await this.$awaitWraper(api.GetRuleByRole());
      let passComplexity = roleData.f_Complexity;
      let passLength = roleData.f_Length;
      if (passLength > value.length) {
        callback(new Error("密码不符合规范"));
      } else {
        // 判断对应密级密码规范
        let check;
        if (passComplexity.indexOf(1) !== -1) {
          // 大写
          check = /^(?=.*[A-Z]).*$/;
          if (!check.test(value)) {
            callback(new Error("密码不符合规范"));
          }
        } else {
          check = /^(?=.*[A-Z]).*$/;
          if (check.test(value)) {
            callback(new Error("密码不符合规范"));
          }
        }
        if (passComplexity.indexOf(2) !== -1) {
          // 小写
          check = /^(?=.*[a-z]).*$/;
          if (!check.test(value)) {
            callback(new Error("密码不符合规范"));
          }
        } else {
          check = /^(?=.*[a-z]).*$/;
          if (check.test(value)) {
            callback(new Error("密码不符合规范"));
          }
        }
        if (passComplexity.indexOf(3) !== -1) {
          // 数字
          check = /^(?=.*\d).*$/;
          if (!check.test(value)) {
            callback(new Error("密码不符合规范"));
          }
        } else {
          // 没有规范数字，判断是否有数字
          check = /^(?=.*\d).*$/;
          if (check.test(value)) {
            callback(new Error("密码不符合规范"));
          }
        }
        if (passComplexity.indexOf(4) !== -1) {
          // 特殊字符
          check =
            /^(?=.*[*?!&￥$%^#,./@";:><\\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
          if (!check.test(value)) {
            callback(new Error("密码不符合规范"));
          }
        } else {
          check =
            /^(?=.*[*?!&￥$%^#,./@";:><\\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
          if (check.test(value)) {
            callback(new Error("密码不符合规范"));
          }
        }
        callback();
      }
    },
    showPassword() {
      this.passwordType == ""
        ? (this.passwordType = "password")
        : (this.passwordType = "");
    },
    async validatorPassword(rule, value, callback) {
      const data = await this.$awaitWraper(
        api.validatorPassword(this.$md5(value))
      );
      if (data) {
        callback();
      } else {
        callback(new Error("密码错误"));
      }
    },
    onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const req = {
            password: this.$md5(this.formData.password),
            oldPassword: this.$md5(this.formData.oldPassword),
          };
          const res = await this.$awaitWraper(api.updatePassword(req));
          if (res) {
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            setTimeout(() => {
              this.$store.dispatch("organization/user/fedLogOut").then(() => {
                this.$router.push({ path: "/login" });
              });
            }, 500);
          }
        }
      });
    },
  },
};
</script>

